package com.sportradar.assessment.work.services;

import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sportradar.assessment.work.beans.ConfigurationBean;
import com.sportradar.assessment.work.util.ConfigUtil;

public class ThreadManager {

	private static Logger logger=LoggerFactory.getLogger(ThreadManager.class);
	
	private NumberProgression numberProgression;
	private Date lastUpdate;
	
	private ConfigurationBean currentConfiguration;
	

	public ThreadManager(NumberProgression numberProgression,ConfigurationBean configuration) {	
		this.numberProgression = numberProgression;
		this.currentConfiguration = configuration;
		this.lastUpdate= new Date();
	}
	
	
	public Date getLastUpdate() {
		return lastUpdate;
	}

	public void setLastUpdate(Date lastUpdate) {
		this.lastUpdate = lastUpdate;
		
	}

	public void updateSettings(ConfigurationBean updatedConfiguration){
		
		this.currentConfiguration = updatedConfiguration;
		if (!currentConfiguration.getCurrentEnabledThreadName().equals(updatedConfiguration.getCurrentEnabledThreadName())){
			//LOG currentThreadChange
		}
		
	}
	


	public synchronized void executeCalculation(String threadName) throws InterruptedException{
	
		while(!threadName.equals(currentConfiguration.getCurrentEnabledThreadName())){
				wait();
		}
		
		TimeUnit.SECONDS.sleep(currentConfiguration.getIntervalBetweenCalculations());
		long nextNumber=numberProgression.getNextNumber();
		System.out.println(threadName+ ":"+nextNumber);
		notifyAll();
	}
	
	
	
	
}
